<?php
class User extends Controller
{
	public function __construct()
    {
        parent::__construct();
        $this->_loader->helper('Html');           
    }

    public function actionIndex()
    {
        return $this->responseView('user/user');
    }

    public function actionFilter()
    {
        $CustomerModel = $this->_loader->model('CustomerModel');
        $CustomerInput = $this->_input->filter(array(
                'name' => Input::STRING,
                'birthday' => Input::STRING,
                'phone' => Input::STRING,
                'email' => Input::STRING,
                'checkedid' =>Input::STRING
                ));
        if(strlen($CustomerInput['checkedid'])>0)
        {
            $CustomerModel->delCustomers($CustomerInput['checkedid']);
        }
        if(strlen($CustomerInput['birthday'])>0)
        {
            $date = explode('-', $CustomerInput['birthday']);
            $CustomerInput['birthday'] = Html::convertdate($date[2],$date[1],$date[0]); 
        }        
        $data['customers'] = $CustomerModel->getCustomers($CustomerInput);
        foreach ($data['customers'] as &$customer) {
            $customer["birthday"] = Html::date($customer["birthday"]);
        } 
        return $this->responseView('ajax/customer',$data);
    }

    protected function _preDispatch($action)
    {
        if (!$this->_session->get('adminid'))
        {
            return $this->responseRedirect('login');
        }
    }

    public function actionAdd()
    {
        $CustomerModel = $this->_loader->model('CustomerModel');
        $CustomerInput = $this->_input->filter(array(
                'add-name' => Input::STRING,
                'add-birthday' => Input::STRING,
                'add-phone' => Input::STRING,
                'add-email' => Input::STRING,
                'add-password' =>Input::STRING
                ));
        if(strlen($CustomerInput['add-email'])>0)
        {
            $date = explode('-', $CustomerInput['add-birthday']);
            $CustomerInput['add-birthday'] = Html::convertdate($date[2],$date[1],$date[0]);            
            $CustomerModel->addCustomer($CustomerInput);
            $customer_id = $CustomerModel->checkEmail($CustomerInput['add-email']);
            return $this->responseRedirect('user/edit/'.$customer_id); 
        }
        else 
        {
            return $this->responseView('user/user_add');
        }       
    }
    public function actioncheckEmail()
    {
        $CustomerModel = $this->_loader->model('CustomerModel');  
        $email = $this->_input->filterSingle('email',Input::STRING);
        $data['result'] = $CustomerModel->checkEmail($email);
        return $this->responseView('ajax/checkemail',$data);     
    }
    public function actionEdit($id)
    {
        $CustomerModel = $this->_loader->model('CustomerModel');
        $CustomerInput = $this->_input->filter(array(
                'customer-name' => Input::STRING,
                'customer-birthday' => Input::STRING,
                'customer-phone' => Input::STRING,
                ));
        if(strlen($CustomerInput['customer-name'].$CustomerInput['customer-birthday'].$CustomerInput['customer-phone'])>0)
        {
            $date = explode('-', $CustomerInput['customer-birthday']);
            $CustomerInput['customer-birthday'] = Html::convertdate($date[2],$date[1],$date[0]);  
            $CustomerModel->updateCustomer($CustomerInput,$id);
        }
        $AddressId = $this->_input->filterSingle('checkedid',Input::STRING);
        if(strlen($AddressId)>0)
        {
            $CustomerModel->deleteAddress($AddressId);
        }
        $NewAddress = $this->_input->filter(array(
            'add-receiver' => Input::STRING,
            'add-phone' => Input::STRING,
            'add-address' =>Input::STRING));
        if(strlen($NewAddress['add-receiver'].$NewAddress['add-phone'].$NewAddress['add-address'])>0)
        {
            $CustomerModel->addAddress($NewAddress,$id);
        }      
        $data['customerinfo'] = $CustomerModel->getCustomerInfo($id);
        $data['customerinfo']['birthday'] = Html::dateslash($data['customerinfo']['birthday']); 
        $data['customeraddress'] = $CustomerModel->getCustomerAddress($id);
        return $this->responseView('user/user_edit',$data);
    }
}
